﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using System.Windows.Threading;
using VisionDemo.Application.MainModels.Models;

namespace VisionDemo.Application.MainModels.ViewModels
{
    public class MainViewModel : ObservableRecipient
    {
        public List<MenuModel> Menus { get; set; }
        MenuModel menu { get; set; }
        public List<LabModel> Lab { get; set; }
        private object _pageContent;

        public object PageContent
        {
            get { return _pageContent; }
            set { SetProperty<object>(ref _pageContent, value); }
        }
        Dictionary<string, object> _pageObjects = new Dictionary<string, object>();
        public ICommand SwitchPageCommand { get; set; }

        private DateTime _currentTime = DateTime.Now;
        
        public DateTime CurrentTime
        {
            get { return _currentTime; }
            set { SetProperty(ref _currentTime, value); }
        }

       

        DispatcherTimer _timer = new DispatcherTimer();
        public MainViewModel()
        {
            _timer.Interval = new TimeSpan(0, 0, 1);
            _timer.Tick += _timer_Tick;
            _timer.Start();
            
            Lab = new List<LabModel>()
            {
                new LabModel { Axis = "X轴:", Value = "--" },
                new LabModel { Axis = "Y轴:", Value = "--" },
                new LabModel { Axis = "Z轴:", Value = "--" },
            };
            Menus = new List<MenuModel>()
            {
                new MenuModel()
                {
                    IsSelected = true,
                    MenuHeader = "主界面",
                    MenuIcon = "\ue63d",
                    TargetView = "RunView"
                },
                new MenuModel()
                {
                    MenuHeader = "运动控制",
                    MenuIcon = "\ue670",
                    TargetView = "TrendPage"
                },
                new MenuModel()
                {
                     MenuHeader = "视觉标定",
                    MenuIcon = "\ue60d",
                    TargetView = "CameraView"
                },
                new MenuModel()
                {
                    
                     MenuHeader = "系统退出",
                    MenuIcon = "\ue630",
                    TargetView = "AlarmPage"
                }
            };

            ShowPage(Menus[0]);
            SwitchPageCommand = new RelayCommand<object>(ShowPage);

        }

        private void ShowPage(object obj)
        {
            var model = obj as MenuModel;
            if (model != null)
            {
                model.IsSelected = false;
                if (menu!=null) menu.IsSelected = true;
                if (model.MenuHeader== "系统退出")
                {
                    System.Windows.Application.Current.Shutdown();
                    return;
                }
                if (PageContent != null &&PageContent.GetType().Name == model.TargetView) return;

                if (_pageObjects.ContainsKey(model.MenuHeader))
                {
                    model.IsSelected = true;
                    if (menu != null) menu.IsSelected = false;
                    menu = model;
                    PageContent = _pageObjects[model.MenuHeader];
                }
                else
                {
                    if (!File.Exists(AppDomain.CurrentDomain.BaseDirectory+ "VisionDemo.Application." + model.TargetView.Replace("View", ".dll"))) return;
                    string path = "VisionDemo.Application." + model.TargetView.Replace("View", "") + ".Views." + model.TargetView;
                    Type type = Assembly.LoadFrom("VisionDemo.Application." + model.TargetView.Replace("View", ".dll")).GetType(path)!;
                    if (type==null) return;
                    object instance = Activator.CreateInstance(type)!;
                    model.IsSelected = true;
                    if (menu != null) menu.IsSelected = false;
                    menu = model;
                    this.PageContent = instance;
                    _pageObjects.Add(model.MenuHeader, instance);
                }

                //if (GlobalUserInfo.UserType == 0 && model.TargetView != "MonitorPage")
                //{
                //    // 提示权限
                //    this.Menus[0].IsSelected = true;
                //    // 提示没有权限操作
                //    if (ActionManager.ExecuteAndResult<object>("ShowRight", null))
                //    {
                //        // 执行重新登录
                //        DoLogout();
                //    }
                //}
                //else
                //{
                //    if (ViewContent != null && ViewContent.GetType().Name == model.TargetView) return;

                //    Type type = Assembly.Load("Zhaoxi.DigitaPlatform.Views")
                //        .GetType("Zhaoxi.DigitaPlatform.Views.Pages." + model.TargetView)!;
                //    ViewContent = Activator.CreateInstance(type)!;
                //}
            }
        }

        private void _timer_Tick(object? sender, EventArgs e)
        {
            CurrentTime = DateTime.Now;
        }
    }
}
